package com.twu.biblioteca;

import com.twu.biblioteca.LibraryContents.Repository;
import com.twu.biblioteca.Users.LoginManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by ppeter on 01/10/2014.
 */
public class Menu {
    private static Menu instance = null;

    private BufferedReader inputStream = null;

    private Menu(){
            inputStream = new BufferedReader(new InputStreamReader(System.in));

    }

    public static Menu getInstance() {
        if (instance == null) {
            instance = new Menu();
        }
        return instance;
    }

    public static void reset(){
        instance = null;
    }

    public void menuLoop(){
        boolean quit = false;
        while(!quit){
            printMenu();
            String selection = getUserInput();
            quit = evaluateUserInput(selection);

        }

    }

    public void whenLoggedInPrint(String loggedIn, String loggedOut){
        if(LoginManager.getInstance().isLoggedIn()) {
            System.out.print(loggedIn);
        } else {
            System.out.print(loggedOut);
        }
    }

    public void printMenu(){
        System.out.println("Please select one from the list: ");
        System.out.println("  (l)ist available books");
        System.out.println("  (c)heck out a book");
        System.out.println("  (r)eturn a book");
        System.out.println("  list available (m)ovies");
        System.out.println("  check out a m(o)vie");
        System.out.println("  return a mo(v)ie");
        whenLoggedInPrint("  lo(g)out\n", "  lo(g)in\n");
        whenLoggedInPrint("  (i)nfo\n", "");

        System.out.println("  (q)uit this program");

    }

    public boolean evaluateUserInput(String menuSelection){
        if(menuSelection.equals("l")){
            System.out.print(Repository.getInstance().listBooks());
            return false;
        }
        if(menuSelection.equals("c")){
            return checkoutBookMenu();
        }
        if(menuSelection.equals("r")){
            return returnBookMenu();
        }
        if(menuSelection.equals("m")){
            System.out.print(Repository.getInstance().listMovies());
            return false;
        }
        if(menuSelection.equals("o")){
            return checkoutMovieMenu();
        }
        if(menuSelection.equals("v")){
            return returnMovieMenu();
        }
        if(menuSelection.equals("q")){
            return true;
        }
        if(menuSelection.equals("g")){
            return loginMenu();
        }
        if(menuSelection.equals("i")){
            return infoMenu();
        }
        System.out.println("Select a valid option!");
        return false;
    }

    private String bookSelectMenu(){
        System.out.println("Please enter the book ID: ");
        return getUserInput();
    }

    public boolean checkoutBookMenu(){
        if(LoginManager.getInstance().isLoggedIn()){
            String bookId = bookSelectMenu();
            if(Repository.getInstance().checkoutBook(bookId, LoginManager.getInstance().getLoggedInUser())){
                System.out.println("Thank you! Enjoy the book");
            } else {
                System.out.println("That book is not available.");
            }

        } else {
            System.out.println("You need to be logged in to checkout books.");
        }
        return false;
    }

    public boolean returnBookMenu(){
        String bookId = bookSelectMenu();
        if(Repository.getInstance().returnBook(bookId)){
            System.out.println("Thank you for returning the book.");
        } else {
            System.out.println("That is not a valid book to return.");
        }
        return false;
    }

    private String movieSelectMenu(){
        System.out.println("Please enter the movie ID: ");
        return getUserInput();
    }

    public boolean checkoutMovieMenu(){
        if(LoginManager.getInstance().isLoggedIn()) {
            String movieId = movieSelectMenu();
            if(Repository.getInstance().checkoutMovie(movieId, LoginManager.getInstance().getLoggedInUser())){
                System.out.println("Thank you! Enjoy the movie");
            } else {
                System.out.println("That movie is not available.");
            }
        } else {
            System.out.println("You need to be logged in to checkout movies.");
        }
        return false;
    }

    public boolean returnMovieMenu(){
        String movieId = movieSelectMenu();
        if(Repository.getInstance().returnMovie(movieId)){
            System.out.println("Thank you for returning the movie.");
        } else {
            System.out.println("That is not a valid movie to return.");
        }
        return false;
    }

    public boolean loginMenu(){
        if(LoginManager.getInstance().isLoggedIn()){
            System.out.println("You are now logged out.");
            LoginManager.getInstance().logout();
        } else {
            System.out.println("Please enter your username: ");
            String username = getUserInput();
            System.out.println("Please enter your password: ");
            String password = getUserInput();

            if(LoginManager.getInstance().login(username,password)){
                System.out.println("You are now logged in.");
            } else {
                System.out.println("You have entered an invalid username/password combination");
            }
        }

        return false;
    }

    public boolean infoMenu(){
        if(LoginManager.getInstance().isLoggedIn()){
            System.out.println(LoginManager.getInstance().getLoggedInUser().getInfo());
        }
        return false;
    }

    public String getUserInput(){
        String userInput = "";
        try {
            userInput = inputStream.readLine();
        } catch (IOException e) {
            System.err.println("Error reading user input: " + e);
        }
        return userInput.trim();
    }
}
